package flc.ast.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelAudioAdapter;
import flc.ast.bean.MyZipBean;
import flc.ast.databinding.ActivitySelAudioBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.media.MediaLoader;
import stark.common.basic.media.bean.AudioBean;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class SelAudioActivity extends BaseAc<ActivitySelAudioBinding> {

    public static int type;
    private SelAudioAdapter selAudioAdapter;
    private int oldPosition = 0;
    private String selPath = "";
    private List<String> listSel = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_sel_audio;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSelAudioBack.setOnClickListener(v -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.tvGoEdit.setOnClickListener(this);

        mDataBinding.rvSelAudioList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        selAudioAdapter = new SelAudioAdapter();
        mDataBinding.rvSelAudioList.setAdapter(selAudioAdapter);
        selAudioAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.tvGoEdit) {//去编辑
            if (TextUtils.isEmpty(selPath) && listSel.size() == 0) {
                ToastUtils.showShort(getString(R.string.please_sel_audio));
                return;
            }
            if (type == 0) {
                VoiceChangeActivity.audioPath = selPath;
                startActivity(VoiceChangeActivity.class);
            } else if (type == 1) {
                AudioPitchSpeedActivity.audioPath = selPath;
                startActivity(AudioPitchSpeedActivity.class);
            } else if (type == 2) {
                AudioFormatActivity.audioPath = selPath;
                startActivity(AudioFormatActivity.class);
            } else if (type == 3) {
                AudioSpliceActivity.mAddList = listSel;
                startActivity(AudioSpliceActivity.class);
            } else if (type == 4) {
                AudioCutActivity.audioPath = selPath;
                startActivity(AudioCutActivity.class);
            } else if (type == 5) {
                List<MyZipBean> list = new ArrayList<>();
                for (String path : listSel) {
                    list.add(new MyZipBean(FileUtils.getFileName(path), ConvertUtils.byte2FitMemorySize(FileUtils.getLength(path), 0)
                            , path, getRandom(50, 20)));
                }
                AudioCompressActivity.listZip = list;
                startActivity(AudioCompressActivity.class);
            }
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (type == 3 || type == 5) {
            if (selAudioAdapter.getItem(position).isSelected()) {
                selAudioAdapter.getItem(position).setSelected(false);
                listSel.remove(selAudioAdapter.getItem(position).getPath());
            } else {
                selAudioAdapter.getItem(position).setSelected(true);
                listSel.add(selAudioAdapter.getItem(position).getPath());
            }
            selAudioAdapter.notifyItemChanged(position);
        } else {
            selAudioAdapter.getItem(oldPosition).setSelected(false);
            selAudioAdapter.notifyItemChanged(oldPosition);
            oldPosition = position;
            selAudioAdapter.getItem(position).setSelected(true);
            selAudioAdapter.notifyItemChanged(position);
            selPath = selAudioAdapter.getItem(position).getPath();
        }
    }

    @Override
    protected void initData() {
        getPermission();
        if (type == 5) {
            mDataBinding.tvGoEdit.setText(getString(R.string.go_compress));
        } else {
            mDataBinding.tvGoEdit.setText(getString(R.string.go_edit));
        }
    }

    //获取权限
    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_AUDIO)
                .reqPermissionDesc(getString(R.string.get_music_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getData();
                    }
                }).request();
    }

    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<AudioBean>>() {
            @Override
            public void doBackground(ObservableEmitter<List<AudioBean>> emitter) {
                List<AudioBean> audioBeans = MediaLoader.loadAudio();
                emitter.onNext(audioBeans);
            }

            @Override
            public void accept(List<AudioBean> audioBeanList) {
                if (audioBeanList != null) {
                    selAudioAdapter.setList(audioBeanList);
                    mDataBinding.rvSelAudioList.setVisibility(View.VISIBLE);
                    mDataBinding.tvSelAudioNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvSelAudioList.setVisibility(View.GONE);
                    mDataBinding.tvSelAudioNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    //生成区间内随机数
    private int getRandom(int maxNum, int minNum) {
        Random random = new Random();
        int i = random.nextInt(maxNum) % (maxNum - minNum + 1) + minNum;
        return i;
    }

}